﻿Public Class frmCustomerAccount
    Dim dbBase As New dbCustomer
    Dim frmCtrl As New FormControl
    Dim Var As New varDeclare
    Dim TypeAct As String
    Dim dsBase As DataSet
    Dim dsGL As DataSet
    Dim EntryID As String
    Dim dsARAccount As DataSet
    Dim dsAPAccount As DataSet

    Private Sub frmCustomerAccount_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dsBase = dbBase.GetCustAccountList()
        Dim ARAccount As String = New dbSystem().FreeQueryString("SELECT AR_ACCOUNT FROM CUSTOMER_ACCOUNT WHERE CUSTOMER_CODE = 0")
        Dim APAccount As String = New dbSystem().FreeQueryString("SELECT AP_ACCOUNT FROM CUSTOMER_ACCOUNT WHERE CUSTOMER_CODE = 0")
        dsARAccount = New dbSystem().FreeQueryDataSet("SELECT GLAC_CODE, GLAC_DESC FROM GL_ACCOUNT WHERE COMP_CODE = '" & Var.CompID & "' AND GLAC_CODE_LEVEL = '" & ARAccount & "'")
        dsAPAccount = New dbSystem().FreeQueryDataSet("SELECT GLAC_CODE, GLAC_DESC FROM GL_ACCOUNT WHERE COMP_CODE = '" & Var.CompID & "' AND GLAC_CODE_LEVEL = '" & APAccount & "'")
        AdvARAccount.Visible = False
        AdvAPAccount.Visible = False
        AdvARAccount.Initialize(dsARAccount, "GLAC_CODE", txtARAccount)
        AdvAPAccount.Initialize(dsAPAccount, "GLAC_CODE", txtAPAccount)
        If (dsBase.Tables(0).Rows.Count() > 0) Then
            ToolbarHeader1.Iniliaze("Entry : Customer Account", Me, Nothing, GridAndFind1.getDataGridView(), "Code")
            GridAndFind1.Initialize(dsBase, "Code", ToolbarHeader1, Me, True)
            GridAndFind1.getDataGridView().Columns("Company").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            Textbox(False, False)
        End If
    End Sub

    Private Sub Textbox(st As Boolean, clear As Boolean)
        txtAPAccount.Enabled = st
        txtAPAccountName.Enabled = st
        txtARAccount.Enabled = st
        txtARAccountName.Enabled = st
        If (st) Then
            st = False
        Else : st = True
        End If
        txtCustCompany.ReadOnly = st
        txtCustCompany.BackColor = Color.White
        txtNPWP.ReadOnly = st
        txtNPWP.BackColor = Color.White
        txtTerms.ReadOnly = st
        txtTerms.BackColor = Color.White
        GridAndFind1.Enabled = st
        If (clear) Then
            txtCustCode.Text = New dbSystem().FreeQueryString("SELECT MAX(CUSTOMER_CODE)+1 Number FROM CUSTOMER")
            txtCustCompany.Text = ""
            txtAPAccount.Text = ""
            txtARAccount.Text = ""
            txtNPWP.Text = ""
            txtTerms.Text = "1"
        End If
    End Sub

    Private Sub ShowAdvARAccount_DoubleClick(sender As Object, e As EventArgs) Handles txtARAccount.DoubleClick, txtARAccountName.DoubleClick
        AdvARAccount.Visible = True
        AdvARAccount.BringToFront()
        AdvARAccount._Show()
    End Sub

    Private Sub ShowAdvAPAccount_DoubleClick(sender As Object, e As EventArgs) Handles txtAPAccount.DoubleClick, txtAPAccountName.DoubleClick
        AdvAPAccount.Visible = True
        AdvAPAccount.BringToFront()
        AdvAPAccount._Show()
    End Sub

    Private Sub txtARAccount_TextChanged(sender As Object, e As EventArgs) Handles txtARAccount.TextChanged
        AdvARAccount.Visible = False
        If (txtARAccount.Text = "load") Then
            txtARAccount.Text = AdvARAccount.dsResult.Tables(0).Rows(0)("GLAC_CODE").ToString()
        ElseIf (txtARAccount.Text.Trim().Length > 4) Then
            Dim dv As DataView = New DataView(dsARAccount.Tables(0))
            dv.RowFilter = "[GLAC_CODE] = '" & txtARAccount.Text.Trim() & "'"
            txtARAccountName.Text = dv.Item(0).Row("GLAC_DESC").ToString()
        Else
            txtARAccountName.Text = ""
        End If
    End Sub

    Private Sub txtAPAccount_TextChanged(sender As Object, e As EventArgs) Handles txtAPAccount.TextChanged
        AdvAPAccount.Visible = False
        If (txtAPAccount.Text = "load") Then
            txtAPAccount.Text = AdvAPAccount.dsResult.Tables(0).Rows(0)("GLAC_CODE").ToString()
        ElseIf (txtAPAccount.Text.Trim().Length > 4) Then
            Dim dv As DataView = New DataView(dsAPAccount.Tables(0))
            dv.RowFilter = "[GLAC_CODE] = '" & txtAPAccount.Text.Trim() & "'"
            txtAPAccountName.Text = dv.Item(0).Row("GLAC_DESC").ToString()
        Else
            txtAPAccountName.Text = ""
        End If
    End Sub

    Private Sub tsNew_Click(ByVal sender As Object) Handles ToolbarHeader1.tsNewClick
        EntryID = ""
        TypeAct = "I"
        Textbox(True, True)
    End Sub

    Private Sub GridCellEnter_Click(ByVal sender As GridAndFind, KeyValue As String) Handles GridAndFind1.GridCellEnter
        Textbox(False, True)
        Dim dvFind As DataView = New DataView(dsBase.Tables(0))
        dvFind.RowFilter = "[Code] = '" & KeyValue & "'"
        EntryID = dvFind.Item(0).Row("Code").ToString()
        txtCustCode.Text = dvFind.Item(0).Row("Code").ToString()
        txtCustCompany.Text = dvFind.Item(0).Row("Company").ToString()
        txtNPWP.Text = dvFind.Item(0).Row("NPWP").ToString()
        txtTerms.Text = dvFind.Item(0).Row("Term Day").ToString()
        txtARAccount.Text = dvFind.Item(0).Row("AR Account").ToString()
        txtAPAccount.Text = dvFind.Item(0).Row("AP Account").ToString()
    End Sub

    Private Sub tsEdit_Click(ByVal sender As Object) Handles ToolbarHeader1.tsEditClick
        TypeAct = "U"
        Textbox(True, False)
    End Sub

    Private Sub tsDelete_Click(ByVal sender As Object) Handles ToolbarHeader1.tsDeleteClick
        If (EntryID.Trim() <> "") Then
            TypeAct = "D"
            Dim result As String = dbBase.CustomerManagement(txtCustCode.Text, txtCustCompany.Text, txtNPWP.Text, txtTerms.Text, txtARAccount.Text, txtAPAccount.Text, TypeAct)
            If (result = "Success") Then
                frmCtrl.openChild(New frmCustomerAccount, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else : MessageBox.Show("Please Choose User First")
        End If
    End Sub

    Private Sub tsPrint_Click(ByVal sender As Object) Handles ToolbarHeader1.tsPrintClick
        ''
    End Sub

    Private Sub tsRefresh_Click(ByVal sender As Object) Handles ToolbarHeader1.tsRefreshClick
        frmCtrl.openChild(New frmCustomerAccount, Me.ParentForm)
    End Sub

    Private Sub tsCancel_Click(ByVal sender As Object) Handles ToolbarHeader1.tsCancelClick
        frmCtrl.openChild(New frmCustomerAccount, Me.ParentForm)
    End Sub

    Private Sub tsSave_Click(ByVal sender As ToolbarHeader) Handles ToolbarHeader1.tsSaveClick
        If (txtCustCompany.Text <> "" Or txtTerms.Text <> "") Then
            Dim result As String = dbBase.CustomerManagement(txtCustCode.Text, txtCustCompany.Text, txtNPWP.Text, txtTerms.Text, txtARAccount.Text, txtAPAccount.Text, TypeAct)
            If (result = "Success") Then
                frmCtrl.openChild(New frmCustomerAccount, Me.ParentForm)
            Else : MessageBox.Show(result)
            End If
        Else
            MessageBox.Show("Please Complete all Field", "InComplete", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End Sub

    Private Sub txtTerms_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtTerms.KeyPress
        Dim txt As TextBox = sender
        Dim FullStop As Char
        FullStop = "."
        If e.KeyChar = FullStop And txt.Text.IndexOf(FullStop) <> -1 Then
            e.Handled = True
            Return
        End If
        If Not Char.IsDigit(e.KeyChar) Then
            If (e.KeyChar <> FullStop) And
               (e.KeyChar <> Convert.ToChar(Keys.Back)) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub

    Private Sub txtTerms_Leave(sender As Object, e As EventArgs) Handles txtTerms.Leave
        Dim txt As TextBox = sender
        If (IsNumeric(txt.Text)) Then
            txt.Text = Convert.ToDecimal(txt.Text).ToString("N0", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        End If
    End Sub

    Private Sub txtTerms_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtTerms.Validating
        Dim txt As TextBox = sender
        If (txt.Text.Trim() <> "") Then
            If (IsNumeric(txt.Text) = False) Then
                e.Cancel = True
            End If
        End If
    End Sub
End Class